import { CustomForm } from "@/components/ProForm/interface";
import dayjs from "dayjs";

export const useForm = () => {
  const addForm = reactive<CustomForm>({
    save: () => null,
    form: {
      date: "",
      text: "",
      type: ""
    },
    options: [
      {
        hide: false,
        label: "基础信息",
        type: "title"
      },
      {
        type: "date_custom",
        hide: false,
        label: "日期",
        prop: "date",
        defaultValue: dayjs().format("YYYY-MM-DD"),
        children: [],
        attrs: {
          type: "date"
        },
        rules: [
          {
            required: true,
            errorMessage: "日期"
          }
        ]
      },
      {
        type: "select",
        hide: false,
        label: "类型",
        prop: "type",
        children: [],
        rules: [
          {
            required: true,
            errorMessage: "类型"
          }
        ]
      },

      {
        type: "textarea",
        label: "备注",
        hide: false,
        prop: "text",
        attrs: {
          disabled: false,
          maxlength: -1
        }
      }
    ]
  });

  onBeforeMount(() => {
    getDictionaries();
  });

  const submitForm = async form => {
    await stockAddDayLog(form);
    useGlobalStore().setMessage();
    setTimeout(() => {
      uni.navigateBack();
    }, 300);
  };

  const getDictionaries = async () => {
    const typeOp: any = await descriptionFindDescriptionByKey({ pid: 3 });
    console.log(typeOp, "typeOptypeOp");

    for (let item of addForm.options) {
      if (item.prop === "type") {
        item.children = changeArrField(typeOp, "dname");
        console.log(item.children, "item.children");
      }
    }
  };

  return {
    addForm,
    submitForm
  };
};
